// pages/order/product-comment/add.js
import http from '../../../utils/http'
import api from '../../../utils/api'
import message from '../../../utils/message';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        image_list:[],
        content:'',
        score:3,
        order_product_id:0


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var order_product_id = options.order_product_id
        //var product_id = options.product_id;
        
        if(order_product_id ==undefined|| order_product_id==null){
            message.show_error( "访问参数异常，无法评论" ,"确定" , function(){
                wx.navigateBack({
                  delta: 1,
                })
            })
        }else{
            this.setData({
                order_product_id:order_product_id
            })
        }


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    contentInput: function (e) {    
        this.setData({
            content: e.detail.value
        });
    },

    setScore: function (e) {
        var score = e.currentTarget.dataset.score;
        this.setData({
            score: score,
        });
    },
    chooseImage:function(){
        //选择并上传图片
        var me = this;

        wx.chooseMedia({
            count: 9,
            mediaType: ['image','video'],
            sourceType: ['album', 'camera'],
            maxDuration: 30,
            camera: 'back',
            success(res) {
                console.log(res);
              //console.log(res.tempFiles.tempFilePath)
              //console.log(res.tempFiles.size)

              if(res.errMsg=="chooseMedia:ok"){
                    var files = res.tempFiles;
                    //var tempFilePaths = res.tempFilePaths;
                    me.uploadImage(files[0].tempFilePath)
              }
                /*
                wx.uploadFile({ 
                    url: '/your_url/receive_file', // 图片上上传的地址，请求方式默认为POST且不可更改
                    filePath: tempFilePaths[0], // 要上传的文件的路径，注：一次只能上传一个文件，若要上传多张图片，请使用递归
                    name: 'file', // 文件对应的键名，后端可以通过这个key获取到文件的二进制内容
                    formData:{
                        'user_id': '123',
                        'name': 'Jack',
                        'age': 18
                    },
                    success: function(res){
                        var data = res.data
                        //do something

                        console.log(data);
                    }
                })

                */


            }
          })

    },
    uploadImage:function( temp_file ){
        console.log("upload_comment_image")
        var me = this;


        api.user.uploadCommentImage( temp_file , {} , function( response ){
            console.log(response);
            var res = JSON.parse(response);
            if(res.status==1){
                var image_list = me.data.image_list;
                image_list.push( res.data.url)

                console.log(image_list);
                me.setData({
                    image_list: image_list
                })
            }
        })

        /*
        http.uploadFile("/common/upload" , temp_file , {} , function ( response ){
            console.log(response);
            var res = JSON.parse(response);
            if(res.status==1){
                var image_list = me.data.image_list;
                image_list.push( res.data.url)

                console.log(image_list);
                me.setData({
                    image_list: image_list
                })
            }
        })

        */
    },

    submitComment:function(){
        var data = {}
        data.content = this.data.content;
        data.score = this.data.score;
        data.image_list = this.data.image_list;
        data.order_product_id = this.data.order_product_id;
        var me = this;

        api.order.submitProductComment( data , function( response ){
            console.log(response)

            if(response.status==1){


                message.action_success("提示" , response.message , "确定" , function(){
                    wx.navigateBack();
                })
            }else{
                message.show_error( response.message )
            }


        })
    },
    doRemoveImage:function (e) {
        var index = e.currentTarget.dataset.index;

        var images_list = this.data.image_list;

        images_list.splice( index ,1 );
        
    }
})